<template>
    <div style="width: 100%; height: 100vh; background-color: #323739; overflow: hidden">
        <div style="width: 400px; margin: 150px auto">
            <div style="color: #cccccc; font-size: 30px; text-align: center; padding: 30px 0">欢迎登录</div>
            <el-form ref="form" :model="form" size="normal" :rules="rules">
                <el-form-item prop="username">
                    <el-input prefix-icon="Avatar" placeholder="用户名" v-model="form.username"/>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input prefix-icon="Lock" placeholder="密码" v-model="form.password" show-password/>
                </el-form-item>
                <el-form-item>
                    <div style="display: flex">
                        <el-input prefix-icon="Key" placeholder="请输入验证码" v-model="form.validCode" style="width: 50%;"/>
                        <ValidCode @input="createValidCode"/>
                    </div>
                </el-form-item>
                <el-form-item>
                    <el-button style="width: 100%" type="primary" @click="login(form)">登 录</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button style="width: 100%" type="primary" @click="this.$router.push('/register')">立即注册
                    </el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import api from "@/api";
    import {ElMessage} from "_element-plus@2.2.18@element-plus";
    import cookie from "@/utils/cookie";
    import ValidCode from "@/components/ValidCode";

    export default {
        name: "Login",
        components: {
            ValidCode
        },
        data() {
            return {
                form: {},
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ]
                },
                validCode: ''
            }
        },
        methods: {
            // 接收验证码
            createValidCode(data) {
                this.validCode = data
            },
            login() {
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        if (!this.form.validCode) {
                            this.$message.error("请输入验证码")
                            return
                        }
                        if (this.form.validCode.toLowerCase() !== this.validCode.toLowerCase()) {
                            this.$message.error("验证码错误")
                            return
                        }
                        api.login(this.form).then(res => {
                            if (res.data.code == "0") {
                                ElMessage({
                                    message: '登陆成功',
                                    type: 'success'
                                })
                                cookie.setCookie("user", JSON.stringify(res.data), 7)
                                if (res.data.data.role === 1) {
                                    this.$router.replace("/user")       // 登陆成功后跳转到管理页
                                } else {
                                    this.$router.replace("/book")
                                }
                            } else {
                                ElMessage.error(res.data.msg)
                            }
                        })
                    }
                })
            },
            // 点击回车键登录
            keyDown(e) {
                // 回车则执行登录方法 enter键的ASCII是13
                if (e.keyCode == 13 || e.keyCode == 100) {
                    this.login(); // 定义的登录方法
                }
            },
        },
        mounted() {
            // 绑定监听事件
            window.addEventListener("keydown", this.keyDown);
        },
        destroyed() {
            // 销毁事件
            window.removeEventListener("keydown", this.keyDown, false);
        },
    }
</script>

<style scoped>

</style>